package org.idansof.otp;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class MetadataSQLOpenHelper extends OrmLiteSqliteOpenHelper {
	
	private static final int DATABASE_VERSION = 6;
	private static final String DATABASE_NAME= "otp";
	public static final String OTP_PROVIDER_NAME = "otp_provider";
	
	private Dao<OTPProvider, Integer> simpleDao = null;	
	
		

	public MetadataSQLOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/**
	 * This is called when the database is first created. Usually you should call createTable statements here to create
	 * the tables that will store your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			Log.i(MetadataSQLOpenHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, OTPProvider.class);

			// here we try inserting data in the on-create as a test
			Dao<OTPProvider, Integer> dao = getOTPProviderDao();
			dao.create(new OTPProvider(
					"otp.hamakor.org.il", 
					"opentripplanner-geocoder/geocode", 
					"opentripplanner-api-webapp/ws/plan", 
					"ישראל - עמותת המקור", 
					"מבוסס על מאגר המידע של משרד התחבורה - מופעל על ידי עמותת המקור לקידום קוד פתוח בישראל", 
					"en-US",33.35,29.419,34.499,35.552));
			dao.create(new OTPProvider(
					"rtp.trimet.org", 
					"geocoder/geocode", 
					"/prod", 
					"Trimet", 
					"Trimet Regional Trip Planner", 
					"en-US",45.6174,45.303,123.0184,122.3537));
			
		} catch (SQLException e) {
			Log.e(MetadataSQLOpenHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * This is called when your application is upgraded and it has a higher version number. This allows you to adjust
	 * the various data to match the new version number.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			Log.i(MetadataSQLOpenHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, OTPProvider.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(MetadataSQLOpenHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached
	 * value.
	 */
	public Dao<OTPProvider, Integer> getOTPProviderDao() throws SQLException {
		if (simpleDao == null) {
			simpleDao = getDao(OTPProvider.class);
		}
		return simpleDao;
	}

	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
		simpleDao = null;
	}	


}
